<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\GroupApplyForm;
use App\Admin\Controllers\Helpers\LinkHelper;
use App\Constant\GroupApplyEnum;
use App\Models\GroupApply;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GroupApplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GroupApply(), function (Grid $grid) {
            $grid->model()->with('user')->orderBy('id', 'desc');
            $grid->quickSearch('id', 'user_id','user.uuid','user.nickname')->placeholder('输入 id\uuid\用户昵称\uid')->auto(false);;

            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function (){
                return LinkHelper::displayUser($this->user);
            });
            $grid->column('type')
                ->using(GroupApplyEnum::TYPE_EXPLAINS)
                ->label(GroupApplyEnum::TYPE_COLORS);
            $grid->column('members')->sortable();
            $grid->column('status')
                ->using(GroupApplyEnum::STATUS_EXPLAINS)
                ->label(GroupApplyEnum::STATUS_COLORS)
                ->sortable();
            $grid->column('applied_at')->sortable();
            $grid->column('action', ' ')
                ->width('100')
                ->display('审核')
                ->modal(function () {
                    /** @var GroupApply $model */
                    $model = $this;

                    return GroupApplyForm::make([
                        'message' => sprintf('%s[%s], 提现 %s 元', $model->user->nickname, $model->user->uuid, $model->money),
                        'withdraw_id' => $model->getAttribute('id')
                    ]);
                });

            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchActions();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GroupApply(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('members');
            $show->field('status');
            $show->field('applied_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GroupApply(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('members');
            $form->text('status');
            $form->text('applied_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
